<template>
  <div class="data-view">
    <el-card>
      <div id="main1"></div>
    </el-card>
    <el-card>
      <div id="main2"></div>
    </el-card>
  </div>
</template>


<script>
import { dataView } from "../../api/api.js";
export default {
  data() {
    return {};
  },
  created() {
    dataView()
      .then((res) => {
        if (res.data.status) {
          let { legend, xAxis, series } = res.data.data;
        //   console.log(res.data.data)
        //   console.log(legend)
        //   console.log(xAxis)
        //   console.log(series)
          this.draw(legend, xAxis, series);
        }
      })
      //抛出异常
      .catch((err) => {
        throw err;
      });
  },
  // 一般绘制图表在mounted里  demo
  mounted() {
    let myChart = this.$echarts.init(document.getElementById("main1")); //初始化实例
    myChart.setOption({
      title: {
        text: "班级",
      },
      tooltip: {},
      xAxis: {
        data: ["一班", "二班", "三班", "四班", "五班", "六班"],
      },
      yAxis: {},
      //数据
      series: [
        {
          name: "人数",
          type: "bar",
          data: [23, 65, 32, 43, 13, 55],
        },
      ],
    });
  },
  methods: {
    draw(legend, xAxis, series) {
      let myChart1 = this.$echarts.init(document.getElementById("main2")); //初始化实例
      let option={
        title: {
          text: "会话量",
        },
        //坐标轴触发，主要在柱状图，折线图等会使用类目轴的图表中使用
        tooltip: {
            trigger:'axis'
        },
        legend:{
          data:legend  //顶部展示
        },
        xAxis: {
          data: xAxis, //底部展示
          type:'category'
        },
        yAxis: {
            type:'value'
        },
        //数据展示
        series:series
      };
      myChart1.setOption(option)
    },
  },
};
</script>


<style lang="scss" scoped>
.data-view {
  width: 100%;
  display: flex;
  justify-content: space-between;
  .el-card {
    width: 50%;
    #main1,
    #main2 {
      height: 500px;
    }
  }
}
</style>
